//@author A0097013L
package gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Timer;
import javax.swing.text.BadLocationException;

import logic.Mark;
import logic.Time;
import memory.GenericEvents;

public class GenericList implements Observer{
	
	ArrayList<String> list;
	private final static String newline = "\n";
	private static final String TASK = "Task";
	private static final String DEADLINE = "Deadline";
	private static final String EVENT = "Event";
	private static final String OPENBRACKETWITHSPACE = " (";
	private static final String LARGE = "large";
	private static final String REDLARGE = "red large";
	private static final String RED = "red";
	private static final String GREENLARGE = "green large";
	private static final String SPACE = " ";
	private static final String CLOSEBRACKET = ")";
	private static final String DASH = " - ";
	private static final String GREEN = "green";
	
	public GenericList() {
		list = new ArrayList<String>();
	}

	@Override
	public void update(Observable o, Object arg) {
		Mark.refreshMark(GuiMain.mem, 0);
		Time.initTimer(GuiMain.mem);
		GuiMain.eventTextPane.setText("");
    	GuiMain.deadlineTextPane.setText("");
    	GuiMain.taskTextPane.setText("");

    	for(int i = 0; i < GuiMain.mem.getSize(); i++) {
			String type = GuiMain.mem.memory.get(i).getType();
			String name = GuiMain.mem.memory.get(i).getName();
			int state = GuiMain.mem.memory.get(i).getState();
			
			if(state!=1) {
				switch(type) {
				case TASK: 
					try {
						GuiMain.taskStyledDoc.insertString(GuiMain.taskStyledDoc.getLength(),name,GuiMain.eventStyledDoc.getStyle(LARGE));
						GuiMain.taskStyledDoc.insertString(GuiMain.taskStyledDoc.getLength(),newline,null);
						GuiMain.taskTextPane.setCaretPosition(0);
					} catch (BadLocationException e1) {
						e1.printStackTrace();
					}
					
					break;
					
				case DEADLINE:
					if(state == 2) { //overdue
						try {
							GuiMain.deadlineStyledDoc.insertString(GuiMain.deadlineStyledDoc.getLength(),name,GuiMain.eventStyledDoc.getStyle(REDLARGE));
							GuiMain.deadlineStyledDoc.insertString(GuiMain.deadlineStyledDoc.getLength(),OPENBRACKETWITHSPACE + GenericEvents.getDate(GuiMain.mem.memory.get(i).getEnd()) + SPACE + GenericEvents.getTime(GuiMain.mem.memory.get(i).getEnd()) + CLOSEBRACKET,GuiMain.eventStyledDoc.getStyle(RED));
							GuiMain.deadlineStyledDoc.insertString(GuiMain.deadlineStyledDoc.getLength(),newline,null);	
							GuiMain.deadlineTextPane.setCaretPosition(0);
						} catch (BadLocationException e) {
							e.printStackTrace();
						}
					} else {
						try {
							GuiMain.deadlineStyledDoc.insertString(GuiMain.deadlineStyledDoc.getLength(),name,GuiMain.eventStyledDoc.getStyle(LARGE));
							GuiMain.deadlineStyledDoc.insertString(GuiMain.deadlineStyledDoc.getLength(),OPENBRACKETWITHSPACE + GenericEvents.getDate(GuiMain.mem.memory.get(i).getEnd()) + SPACE + GenericEvents.getTime(GuiMain.mem.memory.get(i).getEnd()) + CLOSEBRACKET,null);
							GuiMain.deadlineStyledDoc.insertString(GuiMain.deadlineStyledDoc.getLength(),newline,null);	
							GuiMain.deadlineTextPane.setCaretPosition(0);
						} catch (BadLocationException e) {
							e.printStackTrace();
						}
					}
					
					
	
					break;
					
				case EVENT:
					if(state == 2) { //ongoing
						try {
							GuiMain.eventStyledDoc.insertString(GuiMain.eventStyledDoc.getLength(),name,GuiMain.eventStyledDoc.getStyle(GREENLARGE));
							GuiMain.eventStyledDoc.insertString(GuiMain.eventStyledDoc.getLength(),OPENBRACKETWITHSPACE + GenericEvents.getDate(GuiMain.mem.memory.get(i).getStart()) + DASH + GenericEvents.getDate(GuiMain.mem.memory.get(i).getEnd())+ CLOSEBRACKET,GuiMain.eventStyledDoc.getStyle(GREEN));
							GuiMain.eventStyledDoc.insertString(GuiMain.eventStyledDoc.getLength(),newline,null);	
							GuiMain.eventTextPane.setCaretPosition(0);
						} catch (BadLocationException e) {
							e.printStackTrace();
						}
					} else {
						try {
							GuiMain.eventStyledDoc.insertString(GuiMain.eventStyledDoc.getLength(),name,GuiMain.eventStyledDoc.getStyle(LARGE));
							GuiMain.eventStyledDoc.insertString(GuiMain.eventStyledDoc.getLength(),OPENBRACKETWITHSPACE + GenericEvents.getDate(GuiMain.mem.memory.get(i).getStart()) + DASH + GenericEvents.getDate(GuiMain.mem.memory.get(i).getEnd())+ CLOSEBRACKET,null);
							GuiMain.eventStyledDoc.insertString(GuiMain.eventStyledDoc.getLength(),newline,null);	
							GuiMain.eventTextPane.setCaretPosition(0);
						} catch (BadLocationException e) {
							e.printStackTrace();
						}
					}
	
					break;
					
				default:
					break;
				}
			}
			
			changeColour(arg.toString());
			
		}
	}

	private void changeColour(String taskType) {

		switch(taskType) {
		case TASK:
			GuiMain.taskTextPane.setBackground(Color.darkGray);
			
			ActionListener listener = new ActionListener(){
				  public void actionPerformed(ActionEvent event){
					  GuiMain.taskTextPane.setBackground(null);			  }
				};
				
				Timer displayTimer = new Timer(3000, listener);
				displayTimer.setRepeats(false);
				displayTimer.start();
				
				break;
				
		case EVENT:
			GuiMain.eventTextPane.setBackground(Color.darkGray);
			
			ActionListener listener2 = new ActionListener(){
				  public void actionPerformed(ActionEvent event){
					  GuiMain.eventTextPane.setBackground(null);			  }
				};
				
				Timer displayTimer2 = new Timer(3000, listener2);
				displayTimer2.setRepeats(false);
				displayTimer2.start();
				
				break;
			
		case DEADLINE:
			GuiMain.deadlineTextPane.setBackground(Color.darkGray);
			
			ActionListener listener3 = new ActionListener(){
				  public void actionPerformed(ActionEvent event){
					  GuiMain.deadlineTextPane.setBackground(null);			  }
				};
				
				Timer displayTimer3 = new Timer(3000, listener3);
				displayTimer3.setRepeats(false);
				displayTimer3.start();
				
				break;
				
		default:
			break;
		}
		
			
			

	}
	


}
